﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace WeiBeeCommon.DataTypes
{
    public class CommentGenerator : ISerializable
    {
        public List<string> Emote;
        public List<string> Sofa;
        public List<string> Invite;
        public List<string> Splitter;
        private Random random = new Random();
        public CommentGenerator() { }
        public CommentGenerator(SerializationInfo info, StreamingContext context)
        {
            Emote = info.GetValue("Emote", Emote.GetType()) as List<string>;
            Sofa = info.GetValue("Sofa", Sofa.GetType()) as List<string>;
            Invite = info.GetValue("Invite", Invite.GetType()) as List<string>;
            Splitter = info.GetValue("Splitter", Splitter.GetType()) as List<string>;
        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Emote", Emote, Emote.GetType());
            info.AddValue("Sofa", Sofa, Sofa.GetType());
            info.AddValue("Invite", Invite, Invite.GetType());
            info.AddValue("Splitter", Splitter, Emote.GetType());
        }
        public string RandomComment()
        {
            string formater = "{0}{1}{2}{3}{4}";
            string ret = string.Format(formater,
                Emote[random.Next(0, Emote.Count - 1)], Splitter[random.Next(0, Splitter.Count - 1)],
                Sofa[random.Next(0, Sofa.Count - 1)], Splitter[random.Next(0, Splitter.Count - 1)],
                Invite[random.Next(0, Invite.Count - 1)]);
            return ret;
        }

        #region OrderedComment

        private List<string> comments = new List<string>();
        private void InitCommentString()
        {
            string formater = "{0}{1}{2}{3}{4}";
            foreach (string splitterA in Splitter)
            {
                foreach (string splitterB in Splitter)
                {
                    foreach (string emote in Emote)
                    {
                        foreach (string sofa in Sofa)
                        {
                            foreach (string invite in Invite)
                            {
                                comments.Add(string.Format(formater, emote, splitterA, sofa, splitterB, invite));
                            }
                        }
                    }
                }
            }
        }

        private static int _index;
        public string OrderedComment()
        {
            if(comments.Count==0)
            {
                InitCommentString();
            }
            const int d = 100;
            int r = random.Next(0, comments.Count/d); // r >=0, r < comments.Count/100
            string ret = comments[r*d + _index];
            _index = (_index + 1)%d;
            return ret;
        }

        #endregion OrderedComment
    }
}
